home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / idioms.lha / idioms / 9-10.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  831b  |  31 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <a.out.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <stdlib.h>
  10. #include <osfcn.h>
  11.  
  12. caddr_t load(const char *filename) {
  13.     char buf[64];
  14.     caddr_t oadx = (caddr_t)sbrk(0);
  15.     caddr_t adx = ((char*)oadx) + PAGSIZ -
  16.         (((long)oadx) % PAGSIZ);
  17.     sprintf(buf, "ld -N -Ttext %X -A a.out %s -o a.out.new",
  18.         adx, filename);
  19.  
  20.     system(buf);
  21.     int fd = open(filename, O_RDONLY);
  22.     exec Exec;
  23.     read(fd, (char *)&Exec, sizeof(exec));
  24.     sbrk(PAGSIZ - (((long)oadx) % PAGSIZ));
  25.     caddr_t ldadx = (caddr_t)sbrk(Exec.a_text +
  26.         Exec.a_data + Exec.a_bss);
  27.     read(fd, ldadx, Exec.a_text + Exec.a_data);
  28.     close(fd);
  29.     return ldadx;
  30. }
  31.